home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2324 / 2324.xpi / chrome / sessionmanager.jar / content / sessionmanager / sessionexplorer.js < prev    next >
Encoding:
Text File  |  2008-12-04  |  9.9 KB  |  368 lines

  1. function dumpnl(msg) {
  2.     dump(msg + "\n");
  3. }
  4.  
  5. var sessionTreeView = {
  6.     treeBox: null,
  7.     selection: null,
  8.     rowCount: 0,
  9.  
  10.     sessions: null,
  11.     sessionExpanded: [],
  12.     childRow: {
  13.         autosave: "",
  14.         windows: "",
  15.         tabs: "",
  16.         timestamp: "",
  17.         name: "Window names here"
  18.     },
  19.     atom_latest: null,
  20.     atom_autosave: null,
  21.  
  22.     setData: function(data) {
  23.         this.sessions = data;
  24.         for (var i = 0; i < data.length; ++i) {
  25.             this.sessionExpanded[i] = false;
  26.         }
  27.         this.rowCount = data.length;
  28.         dumpnl("rowCount = " + data.length)
  29.         var aserv=Components.classes["@mozilla.org/atom-service;1"]
  30.             .getService(Components.interfaces.nsIAtomService);
  31.         this.atom_latest = aserv.getAtom("latest");
  32.         this.atom_autosave = aserv.getAtom("autosave");
  33.         if (this.treebox)
  34.             this.treebox.invalidate();
  35.     },
  36.     setTree: function(treeBox) {
  37.         dumpnl("setTree(" + treeBox + ")");
  38.         this.treeBox = treeBox;
  39.     },
  40.     getRowWhere: function(predicate) {
  41.         idx = 0;
  42.         for (var i = 0; i < this.sessions.length; ++i) {
  43.             var tmp = {idx: idx, index: i, isContainer: true, data: this.sessions[i]};
  44.             if (predicate(tmp))
  45.                 return tmp;
  46.             if (this.sessionExpanded[i]) {
  47.                 ++idx;
  48.                 var tmp = {idx: idx, isContainer: false, data: this.childRow};
  49.                 if (predicate(tmp))
  50.                     return tmp;
  51.             }
  52.             ++idx;
  53.         }
  54.         dumpnl("getRowWhere: NOT FOUND")
  55.         return {};
  56.     },
  57.     getRow: function(idx) {
  58.         for (var i = 0; i < this.sessions.length; ++i) {
  59.             if (idx-- == 0)
  60.                 return {index: i, isContainer: true, data: this.sessions[i]};
  61.             if (this.sessionExpanded[i] && idx-- == 0)
  62.                 return {isContainer: false, data: this.childRow};
  63.         }
  64.         dumpnl("getRow: NOT FOUND")
  65.         return {};
  66.     },
  67.     getCellText: function(idx, column) {
  68. //        dumpnl("getCellText(" + idx + ", " + column.index + ")");
  69.         var row = this.getRow(idx);
  70.         var session = row.data;
  71.         switch (column.index) {
  72.             case 0: return "";
  73.             case 1: return session.windows;
  74.             case 2: return session.tabs;
  75.             case 3:
  76.                 if (row.isContainer)
  77.                     return yyyy_mm_dd_hh_mm(new Date(session.timestamp));
  78.                 return session.timestamp;
  79.             case 4: return session.name;
  80.         }
  81.         return "?";
  82.     },
  83.     getCellProperties: function(idx, column, prop) {
  84. //        dumpnl("getCellProperties(" + idx + ", " + column.index + ")");
  85.         var session = this.getRow(idx).data;
  86.         if (this.sessions.latestName == session.name)
  87.             prop.AppendElement(this.atom_latest);
  88.         if (column.index == 0 && session.autosave == "session")
  89.             prop.AppendElement(this.atom_autosave);
  90.     },
  91.     isContainer: function(idx) {
  92. //        dumpnl("isContainer(" + idx + ")");
  93.         return this.getRow(idx).isContainer;
  94.     },
  95.     isContainerOpen: function(idx) {
  96. //        dumpnl("isContainerOpen(" + idx + ")");
  97.         return this.sessionExpanded[this.getRow(idx).index];
  98.     },
  99.     isContainerEmpty: function(idx) {
  100. //        dumpnl("isContainerEmpty(" + idx + ")");
  101.         return false;
  102.     },
  103.     isSeparator: function(idx) {
  104. //        dumpnl("isSeparator(" + idx + ")");
  105.         return false;
  106.     },
  107.     isSorted: function() {
  108. //        dumpnl("isSorted()");
  109.         return false;
  110.     },
  111.     isEditable: function(idx, column) {
  112. //        dumpnl("isEditable(" + idx + ", ", column + ")");
  113.         return false;
  114.     },
  115.     getParentIndex: function(idx) {
  116. //        dumpnl("getParentIndex(" + idx + ")");
  117.         if (this.isContainer(idx)) return -1;
  118.         return idx - 1;
  119.     },
  120.     getLevel: function(idx) {
  121. //        dumpnl("getLevel(" + idx + ")");
  122.         if (this.isContainer(idx)) return 0;
  123.         return 1;
  124.     },
  125.     hasNextSibling: function(idx, after) {
  126.         var row = this.getRow(idx);
  127.         if (!row.isContainer)
  128.             return false;
  129.         return row.index < this.sessions.length - 1;
  130.     },
  131.     toggleOpenState: function(idx) {
  132.         var row = this.getRow(idx);
  133.         if (!row.isContainer)
  134.             return;
  135.         var isOpen = this.sessionExpanded[row.index];
  136.         if (this.sessionExpanded[row.index])
  137.             --this.rowCount;
  138.         else
  139.             ++this.rowCount;
  140.  
  141.         this.treeBox.rowCountChanged(idx + 1, isOpen?-1:1);
  142.         this.sessionExpanded[row.index] = !isOpen;
  143.     },
  144.  
  145.     getImageSrc: function(idx, column) {},
  146.     getProgressMode : function(idx,column) {},
  147.     getCellValue: function(idx, column) {},
  148.     cycleHeader: function(col, elem) {},
  149.     selectionChanged: function() {},
  150.     cycleCell: function(idx, column) {},
  151.     performAction: function(action) {},
  152.     performActionOnCell: function(action, index, column) {},
  153.     getColumnProperties: function(column, element, prop) {},
  154.     getRowProperties: function(idx, prop) {}
  155. };
  156.  
  157.  
  158. function zerofill(x, w) {
  159.     return ("0000000000" + x).slice(-w);
  160. }
  161.  
  162. function yyyy_mm_dd_hh_mm(d) {
  163.     return (
  164.         zerofill(d.getFullYear(),4) + "-" +
  165.         zerofill(d.getMonth(),2) + "-" +
  166.         zerofill(d.getDate(),2) + " " +
  167.         zerofill(d.getHours(),2) + ":" +
  168.         zerofill(d.getMinutes(),2)
  169.     );
  170. }
  171.  
  172. function getSelectedSession() {
  173.     var tree = document.getElementById("sessiontree");
  174. //    var view = tree.view;
  175.     var view = sessionTreeView;
  176.     var row = view.getRow(tree.currentIndex);
  177.     if (row.isContainer)
  178.         return row.data;
  179.     return view.getRow(tree.currentIndex-1).data;
  180. }
  181.  
  182. //
  183. // Prompt handling
  184. //
  185. function stdMode() {
  186.     document.getElementById("stdMode").setAttribute("disabled", false);
  187.  
  188.     var ok_button = document.getElementById("ok_button");
  189.     ok_button.removeEventListener("command", ok_button.my_listener, false);
  190.  
  191.     document.getElementById("key_input_mode_cancel")
  192.         .setAttribute("disabled", "true");
  193.  
  194.     document.getElementById("sessiontree").focus();
  195.  
  196.     document.getElementById("prompt").setAttribute("hidden", "true");
  197. }
  198.  
  199. function onTextboxInput() {
  200.     var sessions = sessionTreeView.sessions;
  201.     var ok_button = document.getElementById("ok_button");
  202.     var input = document.getElementById("text_box").value;
  203.     for (var i = 0; i < sessions.length; ++i)
  204.         if (sessions[i].name == input) {
  205.             ok_button.setAttribute("disabled", "true");
  206.             return;
  207.         }
  208.     ok_button.setAttribute("disabled", "false");
  209. }
  210.  
  211. function inputMode(label, ok_func, initial_value) {
  212.     document.getElementById("stdMode").setAttribute("disabled", true);
  213.  
  214.     var ok_button = document.getElementById("ok_button");
  215.     ok_button.setAttribute("label", label);
  216.     ok_button.addEventListener("command", ok_func, false);
  217.     ok_button.my_listener = ok_func;
  218.  
  219.     document.getElementById("key_input_mode_cancel")
  220.         .setAttribute("disabled", "false");
  221.  
  222.     document.getElementById("prompt").setAttribute("hidden", "false");
  223.  
  224.     var text_box = document.getElementById("text_box");
  225.     text_box.value = initial_value? initial_value : getSelectedSession().name;
  226.     text_box.focus();
  227. }
  228.  
  229. function onCancel() {
  230.     stdMode();
  231. }
  232.  
  233. //
  234. // Cmd handlers
  235. //
  236.  
  237. function onCmdOpen(mode) {
  238.     var gSessionManager = window.opener.gSessionManager; 
  239.     gSessionManager.load(getSelectedSession().fileName, mode);
  240. }
  241.  
  242. function onCmdRenameOK() {
  243.     var new_name = document.getElementById("text_box").value; 
  244.     dumpnl(
  245.         "rename '" + getSelectedSession().fileName + "' -> '" +
  246.         new_name + "'"
  247.     );
  248.     var gSessionManager = window.opener.gSessionManager;
  249.     gSessionManager.renameSession(getSelectedSession().fileName, new_name);
  250.     sessionTreeView.setData(window.opener.gSessionManager.getSessions());
  251.  
  252.     // select the renamed row again
  253.     var r = sessionTreeView.getRowWhere(
  254.         function(row){return row.data.name==new_name}
  255.     );
  256.     sessionTreeView.selection.select(r.idx);
  257.  
  258.     stdMode();
  259. }
  260.  
  261. function onCmdRename(label) {
  262.     inputMode(label, onCmdRenameOK);
  263. }
  264.  
  265. function onCmdDelete() {
  266.     dumpnl("delete " + getSelectedSession().fileName);
  267.     var gSessionManager = window.opener.gSessionManager;
  268.     gSessionManager.remove(getSelectedSession().fileName);
  269.     sessionTreeView.setData(window.opener.gSessionManager.getSessions());
  270.     document.getElementById("sessiontree").focus();
  271. }
  272.  
  273. function onCmdSaveOK() {
  274.     alert("save as " + document.getElementById("text_box").value + " (not implemented yet)");
  275. }
  276.  
  277. function onCmdSave() {
  278.     var name = window.opener.gSessionManager.getFormattedName(
  279.         window.opener.content.document.title || "about:blank",
  280.         new Date()
  281.     );
  282.     inputMode("Save", onCmdSaveOK, name);
  283. }
  284.  
  285. function onCmdSaveWindowOK() {
  286.     alert("save window as " + document.getElementById("text_box").value + " (not implemented yet)");
  287. }
  288.  
  289. function onCmdSaveWindow() {
  290.     var name = window.opener.gSessionManager.getFormattedName(
  291.         window.opener.content.document.title || "about:blank",
  292.         new Date()
  293.     );
  294.     inputMode("Save Window", onCmdSaveWindowOK, "Window: " + name);
  295. }
  296.  
  297. function onCmdOpenFolder() {
  298.     window.opener.gSessionManager.openFolder();
  299. }
  300.  
  301. function isColumnCropped(tree, col) {
  302.     var treebox = tree.boxObject;
  303.     treebox.QueryInterface(Components.interfaces.nsITreeBoxObject);
  304.  
  305.     //var treebox = col.nsITreeColumns.nsITreeBoxObject;
  306.     //var tree = ??
  307.  
  308.     for (var i = 0; i < tree.view.rowCount; ++i) {
  309.         if (treebox.isCellCropped(i, col))
  310.             return true;
  311.     }
  312.     return false;
  313. }
  314.  
  315. // There must be a better way!!!
  316. function autoFitColumn(tree, col) {
  317.     var treebox = tree.boxObject;
  318.     treebox.QueryInterface(Components.interfaces.nsITreeBoxObject);
  319.  
  320.     // this works for me...
  321.     col.element.setAttribute("width", 93);
  322.     return;
  323.  
  324.     var min_width = 0;
  325.     while (isColumnCropped(tree, col)) {
  326.         min_width = col.width;
  327.         col.element.setAttribute("width", 2 * col.width);
  328.         // sigh, without this alert the width isn't updated...
  329.         alert("");
  330.     }
  331.     var max_width = col.width;
  332.  
  333.     // find the optimal width by binary search
  334.     var w;
  335.     while (min_width < max_width) {
  336.         w = Math.round((min_width + max_width) / 2);
  337.         col.element.setAttribute("width", w);
  338.  
  339.         // sigh, without this alert the width isn't updated...
  340.         alert("");
  341.  
  342.         if (isColumnCropped(tree, col))
  343.             min_width = w + 1;
  344.         else
  345.             max_width = w - 1;
  346.     }
  347.     if (min_width != max_width) dumpnl(min_width + "!=" + max_width);
  348.  
  349.     col.element.setAttribute("width", min_width);
  350. }
  351.  
  352. function onLoad() {
  353.     window.addEventListener("unload", onUnload, false);
  354.  
  355.     sessionTreeView.setData(window.opener.gSessionManager.getSessions());
  356.     document.getElementById("sessiontree").view = sessionTreeView;
  357.     var tree = document.getElementById("sessiontree");
  358.  
  359.     autoFitColumn(tree, tree.columns.getColumnAt(3));
  360.     
  361.     tree.view.selection.select(0);
  362.     tree.focus();
  363. }
  364.  
  365. function onUnload() {
  366.     dumpnl("unloaded");
  367. }
  368.